﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MsgCenter.DAL.Base;
using Utility;
using Utility.Model;
using System.Data;
using Hxj.Data;

namespace MsgCenter.Service.BLL
{
    public class MsgBLLService
    {
        private static MsgBLLService dataService;
        public static MsgBLLService CreateDataService()
        {
            if (dataService == null)
            {
                dataService = new MsgBLLService();
            }
            return dataService;
        }
        BaseService dal = new BaseService();
        public List<Sys_User> GetBirthUserList()
        {
            string strSql = string.Empty;
            int Year = DateTime.Now.Year;
            int Month = DateTime.Now.Month;
            int Day = DateTime.Now.Day;
            int DaysInMonth = DateTime.DaysInMonth(Year, Month);
            if (Month == 2)
            {
                if (DaysInMonth == 28)
                {
                    if (Day == 28)
                    {
                        strSql = "select * from Sys_User where Month(birthday)=" + Month.ToString() + " and (DAY(birthday)=28 or DAY(birthday)=29)";
                    }
                    else
                    {
                        strSql = "select * from Sys_User where Month(birthday)=" + Month.ToString() + " and DAY(birthday)=" + Day.ToString() + "";
                    }
                }
                else
                {
                    strSql = "select * from Sys_User where Month(birthday)=" + Month.ToString() + " and DAY(birthday)=" + Day.ToString() + "";
                }
            }
            else
            {
                strSql = "select * from Sys_User where Month(birthday)=" + Month.ToString() + " and DAY(birthday)=" + Day.ToString() + "";
            }
            strSql += " and ISNULL(birthday,'')<>'' and UserID not in ( select BussinessID from TB_SMS_Message where DATEDIFF(MONTH, SendDate, '" + DateTime.Now.DateTimeToyyyy_MM_dd() + "')=0 )";
            List<Sys_User> list = new List<Sys_User>();
            using (IDataReader dr = dal.GetDataReader(strSql))
            {
                Sys_User userModel = null;
                while (dr.Read())
                {
                    userModel = new Sys_User();
                    userModel.SetPropertyValues(dr);
                    list.Add(userModel);
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }

        public List<TB_SMS_Message> GetMessageList(string strSendType)
        {
            Hxj.Data.WhereClip wcp = new Hxj.Data.WhereClip();
            wcp = wcp.And(TB_SMS_Message._.Status == 0
                && TB_SMS_Message._.IsInstant != 1
                && TB_SMS_Message._.SendType == strSendType
                && TB_SMS_Message._.SendDate <= DateTime.Now.DateTimeToyyyy_MM_dd_HH_mm_ss());
            if (!strSendType.IsNullOrEmpty())
            {
                if (strSendType == "短信")
                {
                    wcp = wcp.And(TB_SMS_Message._.Mobile1.IsNull("") != "" || TB_SMS_Message._.Mobile2.IsNull("") != "");
                }
            }
            return GetMessageList(wcp, new OrderByClip(TB_SMS_Message._.SendDate, OrderByOperater.DESC));
        }

        public List<TB_SMS_Message> GetMessageList(WhereClip wcp, OrderByClip order)
        {
            return dal.GetList<TB_SMS_Message>(wcp, order);
        }

        public void UpdateMessageStatus(string strSmsID, int strStatus)
        {
            dal.ExecuteNonQuery("Update TB_SMS_Message set Status='" + strStatus.ToString() + "',IsRead=1 where SMSID='" + strSmsID + "'");
        }

        public void InsertMessage(TB_SMS_Message message)
        {
            dal.Save(message);
        }

        public void InsertMessageLog(TB_SMS_Message message, string strHttpStatus)
        {
            TB_SMS_Log tB_SMS_Log = new TB_SMS_Log();
            tB_SMS_Log.Mobile = message.Mobile1;
            tB_SMS_Log.QQ = message.QQ;
            tB_SMS_Log.RtxLoginName = message.RtxLoginName;
            tB_SMS_Log.SendDate = message.SendDate;
            tB_SMS_Log.SendLoginName = message.SendLoginName;
            tB_SMS_Log.SendMessage = message.Messge;
            tB_SMS_Log.SendStatus = strHttpStatus;
            tB_SMS_Log.SendType = message.SendType;
            tB_SMS_Log.SendUserName = message.SendUserName;
            tB_SMS_Log.SMSID = message.SMSID;
            tB_SMS_Log.SystemCode = message.SystemCode;
            tB_SMS_Log.WebChat = message.WebChat;
            InsertMessageLog(tB_SMS_Log);
        }

        public void InsertMessageLog(TB_SMS_Log log)
        {
            dal.Save(log);
        }

        public int GetExecuteScalar(string strCount)
        {
            return dal.ToScalar<int>(strCount);
        }

        public TB_SyncInfo_Data GetSyncInfoData(WhereClip wcp)
        {
            return dal.GetModel<TB_SyncInfo_Data>(wcp);
        }

        public TB_SyncInfo_Data GetSyncInfoData()
        {
            return dal.GetModel<TB_SyncInfo_Data>("1=1");
        }

        public void UpdateSyncInfoOperationTime(string strOperationTime, DateTime dtNow, WhereClip wcp)
        {
            if (strOperationTime == "User")
            {
                dal.GetDataBase().Update<TB_SyncInfo_Data>(TB_SyncInfo_Data._.operationTime_User, dtNow.DateTimeToyyyy_MM_dd_HH_mm_ss(), wcp);
            }
            else if (strOperationTime == "Org")
            {
                dal.GetDataBase().Update<TB_SyncInfo_Data>(TB_SyncInfo_Data._.operationTime_Org, dtNow.DateTimeToyyyy_MM_dd_HH_mm_ss(), wcp);
            }
            else if (strOperationTime == "Dic")
            {
                dal.GetDataBase().Update<TB_SyncInfo_Data>(TB_SyncInfo_Data._.operationTime_Dic, dtNow.DateTimeToyyyy_MM_dd_HH_mm_ss(), wcp);
            }
        }

        public Sys_User GetUserModel(WhereClip wcp)
        {
            return dal.GetModel<Sys_User>(wcp);
        }

        public void Save(Sys_User model, Sys_UserRoles modeluserrole)
        {
            DbTrans dbTrans = dal.GetDataBase().BeginTransaction();
            try
            {
                dal.Save(model, dbTrans);
                if (modeluserrole != null)
                {
                    dal.Save(modeluserrole, dbTrans);
                }
                dbTrans.Commit();
            }
            catch (Exception ex)
            {
                dbTrans.Rollback();
                throw ex;
            }
            finally
            {
                dbTrans.Close();
            }
        }

        public Sys_Dept GetDeptModel(WhereClip wcp)
        {
            return dal.GetModel<Sys_Dept>(wcp);
        }

        public void Save(Sys_Dept model)
        {
            DbTrans dbTrans = dal.GetDataBase().BeginTransaction();
            try
            {
                dal.Save(model, dbTrans);
                dbTrans.Commit();
            }
            catch (Exception ex)
            {
                dbTrans.Rollback();
                throw ex;
            }
            finally
            {
                dbTrans.Close();
            }
        }

        public int GetMaxSortByFatherID(string FatherID)
        {
            int num = 0;
            string strSql = "select max(sort)+1 from Sys_Dept where ParentID='" + FatherID + "'";
            object obj = dal.ToScalar<object>(strSql);
            if (!object.Equals(obj, null) && !object.Equals(obj, DBNull.Value))
            {
                return obj.ToSafeInt();
            }
            return 1;
        }

        public string GetMaxDeptIDByFatherID(string FatherID)
        {
            string strSql = "select max(deptid) from Sys_Dept where ParentID='" + FatherID + "'";
            object obj = dal.ToScalar<object>(strSql);
            string text = string.Empty;
            if (obj != null)
            {
                text = obj.ToString();
            }
            return text.ToString();
        }

        public string GetMaxDeptIDByFather(string DeptId)
        {
            string text = GetMaxDeptIDByFatherID(DeptId);
            if (text.Length <= 0)
            {
                return DeptId + "00001";
            }
            string str = string.Empty;
            if (text.Length > 5)
            {
                str = text.Substring(0, text.Length - 5);
                text = text.Substring(text.Length - 5, 5);
            }
            ulong num = Convert.ToUInt64(text) + 1;
            switch (num.ToString().Length)
            {
                case 1:
                    text = "0000" + num.ToString();
                    break;
                case 2:
                    text = "000" + num.ToString();
                    break;
                case 3:
                    text = "00" + num.ToString();
                    break;
                case 4:
                    text = "0" + num.ToString();
                    break;
            }
            return str + text;
        }

        public int GetDicMaxSort(string dicParentId)
        {
            string strSql = "select ISNULL(MAX(sort),0)+1 as sort from Sys_Dic where ParentDicID='" + dicParentId + "'";
            return dal.ToScalar<int>(strSql);
        }

        public Sys_Dic GetDicModel(WhereClip wcp)
        {
            return dal.GetModel<Sys_Dic>(wcp);
        }
        public void Save(Sys_Dic model)
        {
            DbTrans dbTrans = dal.GetDataBase().BeginTransaction();
            try
            {
                dal.Save(model, dbTrans);
                dbTrans.Commit();
            }
            catch (Exception ex)
            {
                dbTrans.Rollback();
                throw ex;
            }
            finally
            {
                dbTrans.Close();
            }
        }
    }
}